//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using Er = Microsoft.Research.DataLayer;
    using System.Reflection;
    using TridentAPI;
    using Microsoft.Research.ScientificWorkflow.TridentComposer;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using Microsoft.Practices.EnterpriseLibrary.Logging;
    using System.IO;

    /// <summary>
    /// Interaction logic for MyModalWindow.xaml
    /// </summary>
    public partial class TypeInitializerContainer : Window
    {
        /// <summary>
        /// Initializer for accessing the provider services.
        /// </summary>
        private BaseTypeInitializer initializer;

        /// <summary>
        /// The container for the custom type initializer.
        /// </summary>
        /// <param name="c">
        /// The registry connection element.
        /// </param>
        public TypeInitializerContainer()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.ShowInTaskbar = false;
            this.Owner = Application.Current.MainWindow;
            this.ResizeMode = ResizeMode.NoResize;
            this.SizeToContent = SizeToContent.WidthAndHeight;
        }

        /// <summary>
        /// This method loads the type initializer control for the
        /// type hosted inside this window and displays the same to the
        /// user. The user has to exit this dialog to get back to the
        /// main UI.
        /// </summary>
        public BaseTypeInitializer DoModal(Er.TypeInitializer typeInit, string deserializedValue)
        {
            try
            {
                this.initializer = ActivityComposer.LoadInitializer(typeInit, deserializedValue);
                if (this.initializer != null)
                {
                    UserControl uiElem = initializer.GetUIControl();
                    this.DialogGrid.Children.Add(uiElem);
                    this.ShowDialog();
                }
            }
            catch (TridentCustomException ex)
            {
                TridentMessageBox.ShowTridentErrorMessageBox(ex.Message);
            }

            return this.initializer;
        }

        /// <summary>
        /// Fired on closing of the window.
        /// </summary>
        /// <param name="e">
        /// Args specifying the behavior.
        /// </param>
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (this.initializer != null)
            {
                this.initializer.SaveValuesIntoCustomObject();
            }

            base.OnClosing(e);
        }
    }
}
